use axum::Json;
use http::HeaderMap;
use serde_json::Value;

use crate::{code, query};

// 下单
pub async fn place_order(header:HeaderMap, serial_no:&str, data:Value ) -> Result<String, Json<Value>>{
    let url = "/v3/pay/transactions/jsapi";
    let rs = super::place_order(header, url, serial_no, data ).await?;
    let prepay_id = query::str_op(&rs, "prepay_id")?;
    if let Some(f) = prepay_id {
        Ok(f)
    }else{
        Err(code::err_(query::str(&rs, "message")?))
    }
}
// 下单并支付
pub async fn place_order_pay(header:HeaderMap, appid:&str, serial_no:&str, data:Value ) -> Result<Json<Value>, Json<Value>>{
    let prepay_id = place_order(header, serial_no, data ).await?;
    super::payment(appid, &prepay_id).await
}